 ///
 /// @file    io3.cc
 /// @author  lemon(haohb13@gmail.com)
 /// @date    2016-03-02 10:24:58
 ///
 
#include <iostream>
#include <fstream>
#include <string>

using std::cout;
using std::endl;
using std::string;
using std::ifstream;
using std::ofstream;
using std::fstream;


int test0(void)
{
	//std::ios::ate对于输出流不起作用
	//std::ios::app 用于在文件末尾添加数据
	//ofstream ofs("test.txt", std::ios::ate);
	ofstream ofs("test.txt", std::ios::app);
	if(!ofs.good())
	{
		cout << "ofstream open error!" << endl;
		return -1;
	}

	cout << ofs.tellp() << endl;
	ofs << "that's new" << endl;

	ofs.close();

	return 0;
}

int test1(void)
{
	test0();
	
	//std::ios::ate 模式针对输入流来使用的
	ifstream ifs("test.txt", std::ios::ate);
	if(!ifs.good())
	{
		cout << "ifstream open error!" << endl;
		return -1;
	}

	cout << ifs.tellg() << endl;
	ifs.close();
}

int main(void)
{
	fstream fs("f1.dat", std::ios::in | std::ios::out);
	if(!fs.good())
	{
		cout << "fstream open error" << endl;
		return -1;
	}
//	cout << "execute" << endl;
	int ival;
	for(int idx = 0; idx != 10; ++idx)
	{
		std::cin >> ival;
		fs << ival << ' ';
	}
	cout << fs.tellp() << endl;

	fs.seekg(0, std::ios::beg); 
	cout << fs.tellg() << endl;

	for(int idx = 0; idx != 10; ++idx)
	{
		fs >> ival;
		cout << "ival = " << ival << endl;
	}

	fs.close();//不要忘了关闭文件流

	return 0;
}

